/*
* File: index.js
* Date: 2011/11/21 GMT+8
* author:
*
*/

Ext.chart.Chart.CHART_URL = 'extjs/resources/charts.swf';
Ext.BLANK_IMAGE_URL="images/s.gif";
Ext.QuickTips.init();// 启动悬停提示

/** common variable **/
var dashboard_cpu_memory_timer;
var dashboard_traffic_timer;

var homepage_already_clicked	= false;
var dashboard_already_clicked	= false;
var config_already_clicked 	= false;
var stats_already_clicked 	= false;

window.name = 'main_window';

function doAction(e, t){
	var url = t.rel;
	center	= Ext.getCmp("centerPanel");

	if(url){
		center.items.each(function(c) { center.remove(c); });

		var p = new Ext.Panel({
			layout: 'anchor',
			anchor: "100% 100%",
			autoScroll: true,
			style: 'background:#F1F1F1;',
			bodyStyle: 'border:0px; background:#F1F1F1;',
			id: "index",
			html: '<iframe frameborder="0" style="margin:0px;padding:0px;background:#F1F1F1;" width="100%" height="100%" src=' + url + ' />'
		});
		center.add(p);
		center.doLayout();
	} else {
		var config = messagebox_config_create('警告', '无效的URL链接！',
		true, 400, 400,
		Ext.Msg.OK, Ext.Msg.WARNING);
		Ext.MessageBox.show(config);
	}
}

function homepage_click_handler()
{
	clearInterval(dashboard_cpu_memory_timer);
	clearInterval(dashboard_traffic_timer);

	dashboard_already_clicked 	= false;
	config_already_clicked		= false;
	stats_already_clicked		= false;

	//alert(homepage_already_clicked);
	if (homepage_already_clicked == true)
		return false;
	else{
		homepage_already_clicked = true;
		homepage_handler();
	}
}

function dashboard_click_handler()
{
	clearInterval(dashboard_cpu_memory_timer);
	clearInterval(dashboard_traffic_timer);

	homepage_already_clicked	= false;
	dashboard_already_clicked	= false;
	stats_already_clicked		= false;

	//if (config_already_clicked == true)
	//return;
	//else
	config_already_clicked = true;

	var t = new Object();
	t.rel = 'bin/dashboard.php';	// redirect to system manage page
	doAction('', t);

	Ext.getCmp('action-panel').hide();
	Ext.getCmp('centerPanel').setWidth('100%');
	Ext.getCmp('viewport_id1').doLayout();
}

function config_click_handler()
{
	clearInterval(dashboard_cpu_memory_timer);
	clearInterval(dashboard_traffic_timer);

	homepage_already_clicked	= false;
	dashboard_already_clicked	= false;
	stats_already_clicked		= false;

	//if (config_already_clicked == true)
	//return;
	//else
	config_already_clicked = true;

	var t = new Object();
	t.rel = 'bin/sysadmin.php';	// redirect to system manage page
	doAction('', t);

	Ext.getCmp('action-panel').show();
	Ext.getCmp('centerPanel').setWidth('100%');
	Ext.getCmp('viewport_id1').doLayout();

	config_already_clicked = false;
}

function stats_click_handler()
{
	clearInterval(dashboard_cpu_memory_timer);
	clearInterval(dashboard_traffic_timer);

	homepage_already_clicked	= false;
	dashboard_already_clicked	= false;
	stats_already_clicked		= false;

	//if (config_already_clicked == true)
	//return;
	//else
	config_already_clicked = true;

	var t = new Object();
	t.rel = 'bin/statistics.php';	// redirect to system manage page
	doAction('', t);
	
	Ext.getCmp('action-panel').hide();
	Ext.getCmp('centerPanel').setWidth('100%');
	Ext.getCmp('viewport_id1').doLayout();
}

function download_click_handler()
{
	clearInterval(dashboard_cpu_memory_timer);
	clearInterval(dashboard_traffic_timer);
}

Ext.onReady(function(){


	//initialization();
	Ext.getDoc().on("contextmenu", function(e){
		e.stopEvent();
	});



	var systemManage = new Ext.Panel({
		id: 'system-manage-id',
		cls: 'left-panel-background',
		iconCls: 'system-manage-panel-title-icon',
		frame: false,
		title: '系统管理',
		collapsible:true,
		contentEl:'system-manage',		/* content element */
		titleCollapse: true
	});

	var networkManage = new Ext.Panel({
		cls: 'left-panel-background',
		iconCls: 'network-manage-panel-title-icon',
		frame: false,
		title: '网络管理',
		collapsible:true,
		contentEl:'network-manage',
		titleCollapse: true
	});

	var vserverManage = new Ext.Panel({
		cls: 'left-panel-background',
		iconCls: 'vserver-manage-panel-title-icon',
		frame: false,
		title: '虚拟服务器管理',
		collapsible:true,
		contentEl:'vserver-manage',
		titleCollapse: true
	});

	var policyRuleManage = new Ext.Panel({
		cls: 'left-panel-background',
		iconCls: 'policy-manage-panel-title-icon',
		frame: false,
		title: '策略规则管理',
		collapsible:true,
		contentEl:'policyRule-manage',
		titleCollapse: true
	});


	var systemDiagnosis = new Ext.Panel({
		cls: 'left-panel-background',
		iconCls: 'disgnosis-manage-panel-title-icon',
		frame: false,
		title: '诊断管理',
		collapsible:true,
		contentEl:'system-diagnosis',
		titleCollapse: true
	});

	var login_username 	= getCookie("username");
	var login_role	   	= getCookie("role");
	if(login_role == 'superadmin') login_role = '超级管理员';
	else if (login_role == 'administrator') login_role = '系统管理员';
	else login_role = '系统审计员';
	var appversion		= getCookie('appversion');
	var hbstate		= getCookie('hbstate');
	if(hbstate == 'alone'){
		var hb_state = '单机';
	}else if(hbstate == 'MASTER'){
		var hb_state = '主机';
	}else if(hbstate == 'BACKUP'){
		var hb_state = '备机';
	}
	

	var content_html = ' \
			<table cellspacing="0" cellpadding="0" border="0" width="100%" height=40 class="dstop">  \
			<tbody>	\
				<tr>	\
				<td><img alt="" src="images/logo.jpg"></td> 	\
				<td class="dstop_right">	\
				(<a class="nav" href="javascript:void(0)">' + login_username + '</a>/<a class="nav" href="#">' + login_role + '</a>)\
				<img alt="" src="images/02.gif">	\
				<a target="_top" class="nav" href="logout.php">退出</a>	\
				</td> 	\
				</tr> 	\
			</tbody> 	\
			</table>	\
			<div class="index"> 	\
			<div class="cartabs_table">	\
			<div class="cartabs_left"> 	\
			<span style="margin-left:0px;">软件版本: <span id="system_appversion">' + appversion + '</span></span>	\
			<span style="margin-left:15px;">当前系统状态: <span id="system_state">' + hb_state + '</span></span>	\
			</div>	\
			<div class="cartabs_all"> 	\
				<ul> 	\
					<li class="car1">	\
			<a href="javascript:void(0)" onclick="homepage_click_handler()" id="click_homepage_id" style="text-align:center;">主页</a>\
					</li> 	\
					<li class="car2">	\
			<a href="javascript:void(0)" onclick="dashboard_click_handler()" style="text-align:center;">仪表盘</a>	\
					</li> 	\
					<li class="car3">	\
			<a href="javascript:void(0)" onclick="config_click_handler()" style="text-align:center;">配置</a>	\
					</li> 	\
					<li class="car4">	\
			<a href="javascript:void(0)" onclick="stats_click_handler()" style="text-align:center;">统计</a>	\
					</li>	\
				</ul> 	\
			</div>	\
			</div>'; 

	var top = new Ext.Panel({
		id: 'top_id1',
		region: "north",
		height: 75,
		html: content_html
	});


	// 左边面板
	var left = new Ext.Panel({		/* left control panel */
		id:'action-panel',
		region: "west",
		split: true,			// commented by fanyf
		width:200,
		minWidth: 150,
		frame: false,
		autoScroll: true,
		style: 'background:#F1F1F1;',
		bodyStyle: 'border:0px; background:#F1F1F1;',
		items: [systemManage, networkManage, vserverManage, policyRuleManage, systemDiagnosis]
	});

	// 中间面板
	var center = new Ext.Panel({
		id: "centerPanel",
		layout: 'anchor',
		width: "100%",
		height: "100%",
		region: "center",
		defaults: {autoScroll:true},
		style: 'background:#F1F1F1;',
		bodyStyle: 'border:0px; background:#F1F1F1;',
		autoScroll: true,
		items: [],
		enableTabScroll: true
	});

	// 底部面板
	var bottom = new Ext.Panel({
		region: "south",
		border: true,
		height: 30,
		html: "<a href='http://www.sinogrid.com' style='text-decoration:underline' target='_blank'>北京信诺瑞得软件系统有限公司 Beijing SinoGrid Software Systems Ltd.<span style='font-family: Arial;font-size: 13px;'>&copy;</span> 2012</a>",
		bodyStyle: "font-size: 12px;text-align:center; \
				padding-top:7px; \
				background: none repeat scroll 0 0 #E0E0E0;"
	});


	var vp = new Ext.Viewport({						/* viewport */
		id: 'viewport_id1',
		layout: "border",
		items:[top, left, center, bottom]
	});

	var le = left.body;							/* register event */
	le.on('click', doAction, null, {delegate:'a', preventDefault:true});	/* delegate: a tag */

	homepage_click_handler(); 				// replace portal

	allbuttons_disabled_check();
});
